package com.neuedu.oasystem.service.impl;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

import com.neuedu.oasystem.dao.INoticeDao;
import com.neuedu.oasystem.dao.impl.NoticeDaoImpl;
import com.neuedu.oasystem.entity.Notice;
import com.neuedu.oasystem.service.NoticeService;
import com.neuedu.oasystem.util.DBHelper;

public class NoticeServiceImpl implements NoticeService{
	private INoticeDao noticeDao;

	public NoticeServiceImpl() {
		super();
		this.noticeDao =new NoticeDaoImpl();
	}
	@Override
	public boolean add(Notice param) {
		Connection conn = DBHelper.openConnection();
		try {
			conn.setAutoCommit(false);
			noticeDao.setConnection(conn);
			boolean ret = noticeDao.add(param);
			conn.commit();
			conn.setAutoCommit(true);
			return ret;
		} catch (SQLException e) {
			if (conn!=null) {
				try {
					conn.rollback();
					conn.setAutoCommit(true);
				} catch (SQLException e1) {
					e1.printStackTrace();
				}
				
			}
			e.printStackTrace();
		} finally {
			DBHelper.closeAll(null, null, conn);
		}
		return false;
	}

	@Override
	public boolean edit(Notice param) {
		Connection conn = DBHelper.openConnection();
		try {
			conn.setAutoCommit(false);
			noticeDao.setConnection(conn);
			boolean ret = noticeDao.edit(param);
			conn.commit();
			conn.setAutoCommit(true);
			return ret;
		} catch (SQLException e) {
			if (conn!=null) {
				try {
					conn.rollback();
					conn.setAutoCommit(true);
				} catch (Exception e2) {
				}
			} 
			e.printStackTrace();
		} finally {
			DBHelper.closeAll(null, null, conn);
		}
		return false;
	}

	@Override
	public boolean deleteById(int id) {
		Connection conn = DBHelper.openConnection();
		try {
			conn.setAutoCommit(false);
			noticeDao.setConnection(conn);
			boolean ret = noticeDao.deleteById(id);
			conn.commit();
			conn.setAutoCommit(true);
			return ret;
		} catch (SQLException e) {
			if (conn!=null) {
				try {
					conn.rollback();
					conn.setAutoCommit(true);
				} catch (Exception e2) {
				}
			} 
			e.printStackTrace();
		} finally {
			DBHelper.closeAll(null, null, conn);
		}
		return false;
	}

	@Override
	public Notice findById(int id) {
		
		return null;
	}

	@Override
	public List<Notice> findAll() {
		Connection conn = DBHelper.openConnection();
		try {
			noticeDao.setConnection(conn);
			List<Notice> list = noticeDao.findAll();
			return list;
		} catch (Exception e) {
		} finally {
			DBHelper.closeAll(null, null, conn);
		}
		return null;
	}

	@Override
	public List<Notice> findByExample(Notice param) {
		
		return null;
	}

	@Override
	public List<Notice> findByPager(int page, int size) {
		
		return null;
	}

	@Override
	public int deleteByIds(int[] ids) {
		
		return 0;
	}

	@Override
	public int addAll(List<Notice> useparamrs) {
		
		return 0;
	}

	@Override
	public int editAll(List<Notice> param) {
		
		return 0;
	}
	@Override
	public boolean deleteByTitle(String title) {
		Connection conn = DBHelper.openConnection();
		try {
			conn.setAutoCommit(false);
			noticeDao.setConnection(conn);
			boolean ret = noticeDao.deleteByTitle(title);
			conn.commit();
			conn.setAutoCommit(true);
			return ret;
		} catch (SQLException e) {
			if (conn!=null) {
				try {
					conn.rollback();
					conn.setAutoCommit(true);
				} catch (Exception e2) {
				}
			} 
			e.printStackTrace();
		} finally {
			DBHelper.closeAll(null, null, conn);
		}
		return false;
	}
	@Override
	public boolean editById(int id) {
		
		return false;
	}
	@Override
	public boolean findByTitle(String title) {
		Connection conn = DBHelper.openConnection();
		try {
			conn.setAutoCommit(false);
			noticeDao.setConnection(conn);
			boolean ret = noticeDao.findByTitle(title);
			conn.commit();
			conn.setAutoCommit(true);
			return ret;
		} catch (SQLException e) {
			if (conn!=null) {
				try {
					conn.rollback();
					conn.setAutoCommit(true);
				} catch (SQLException e1) {
					e1.printStackTrace();
				}
				
			}
			e.printStackTrace();
		} finally {
			DBHelper.closeAll(null, null, conn);
		}
		return false;
	}
	@Override
	public boolean findId(int id) {
		Connection conn = DBHelper.openConnection();
		try {
			conn.setAutoCommit(false);
			noticeDao.setConnection(conn);
			boolean ret = noticeDao.findId(id);
			conn.commit();
			conn.setAutoCommit(true);
			return ret;
		} catch (SQLException e) {
			if (conn!=null) {
				try {
					conn.rollback();
					conn.setAutoCommit(true);
				} catch (SQLException e1) {
					e1.printStackTrace();
				}
				
			}
			e.printStackTrace();
		} finally {
			DBHelper.closeAll(null, null, conn);
		}
		return false;
	}

}
